package com.smforj.ssm.modules.api.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smforj.ssm.frame.core.enums.RouterMapping;
import com.smforj.ssm.frame.core.web.controller.BaseControllerSupportImpl;
import com.smforj.ssm.frame.core.web.domain.DataResult;
import com.smforj.ssm.modules.lib.domain.SysBook;
import com.smforj.ssm.modules.lib.domain.trolley.InventoryRequest;
import com.smforj.ssm.modules.lib.domain.trolley.MissionBook;
import com.smforj.ssm.modules.lib.domain.trolley.MissionRequest;
import com.smforj.ssm.modules.lib.domain.trolley.MissionResponse;
import com.smforj.ssm.modules.lib.domain.trolley.SynchronizeRequest;
import com.smforj.ssm.modules.lib.domain.trolley.SynchronizeResponse;
import com.smforj.ssm.modules.lib.service.BookInfoService;
import com.smforj.ssm.modules.lib.service.MissionService;


/**
 * 盘点车API接口Controller
 * @author jadeshi
 * @date 2017/11/28 15:28
 * @name TrolleyController.java
 * @package com.smforj.ssm.restful.web.controller
 **/
@RouterMapping("")
@Controller
@RequestMapping("/trolley/api/editing")
public class TrolleyController extends BaseControllerSupportImpl {
    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    @Qualifier("sysBookInfoService")
    private BookInfoService bookInfoService;

    @Autowired
    private MissionService missionService;


    /**
     * 同步到本地
     * @date 2017/11/28 15:26
     * @method SynchronizeDown
     * @param [request]
     * @return com.smforj.ssm.frame.core.web.domain.DataResult<java.util.List<com.smforj.ssm.sys.domain.SysBook>>
     **/
    @RequestMapping(value = {"/synchronizeDown"}, method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public DataResult<List<SysBook>> SynchronizeDown(@RequestBody SynchronizeRequest request) {
        DataResult<List<SysBook>> result = new DataResult<List<SysBook>>();
        List<SynchronizeResponse> responseList = new ArrayList<SynchronizeResponse>();
//        SynchronizeResponse synchronizeResponse = null;
        String shelfName = request.getShelfName();
        SysBook sysBook = new SysBook();
        try {
            if (!shelfName.equals("")) {
                sysBook.setShiefname(shelfName);
            }
            List<SysBook> stationResult = bookInfoService.queryList(sysBook);
            if (stationResult != null && stationResult.size() != 0) {
                result.addOK("同步成功", stationResult);
            } else {
                result.addError("无图书信息");
            }
        } catch (Exception ex) {
            result.addError("同步失败");
        }
        return result;
    }


    /**
     * 
     * @date 2017/11/28 15:29
     * @method SynchronizeUp
     * @param [request]
     * @return com.smforj.ssm.frame.core.web.domain.DataResult
     **/
    @RequestMapping(value = {"/synchronizeUp"}, method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public DataResult SynchronizeUp(@RequestBody SynchronizeRequest request) {
        DataResult result = new DataResult();
        List<SysBook> books = request.getBookList();
        try {
            bookInfoService.updateInBatch(books);
            result.setStatus(DataResult.DataStatus.OK);
            result.setMessage("更新远程数据库成功");
        } catch (Exception ex) {
            result.addError("更新远程数据库失败" + ex.toString());
        }
        return result;
    }

    /**
     * 
     * @date 2017/11/28 15:59
     * @method GetMission
     * @param [request]
     * @return com.smforj.ssm.frame.core.web.domain.DataResult<java.util.List<MissionBook>>
     **/
    @RequestMapping(value = {"/getMission"}, method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public DataResult<List<MissionBook>> GetMission(@RequestBody MissionRequest request) {
        DataResult<List<MissionBook>> result = new DataResult<List<MissionBook>>();
        List<MissionBook> responseList = null;
        responseList = missionService.getMission(request);
        if (responseList != null && responseList.size() != 0) {
            for (MissionBook missionBook : responseList
                    ) {
                Timestamp date = new Timestamp(System.currentTimeMillis());
                missionBook.setUpdate_date(date);
            }
            result.addOK("下载成功", responseList);
        }
        else
        {
            result.addError("下载失败");
        }
        return result;
    }


    /**
     * 
     * @date 2017/11/28 16:03
     * @method UploadMission
     * @param [request]
     * @return com.smforj.ssm.frame.core.web.domain.DataResult<java.util.List<MissionResponse>>
     **/
    @RequestMapping(value = {"/uploadMission"}, method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public DataResult<List<MissionResponse>> UploadMission(@RequestBody InventoryRequest request) {
        DataResult<List<MissionResponse>> result = new DataResult<List<MissionResponse>>();
        List<MissionResponse> responseList = new ArrayList<MissionResponse>();
        try {
            responseList = missionService.insertBooks(request.getMissionBooks());
            result.addOK("上传成功", responseList);
        } catch (Exception ex) {
            result.addError("上传失败");
        }
        return result;
    }

}
